package com.example.SecondStore.service.impl;

//import cn.hutool.json.JSONUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.SecondStore.controller.CommodityController;
import com.example.SecondStore.dao.CommodityDao;
import com.example.SecondStore.entity.Commodity;
import com.example.SecondStore.service.CommodityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.SecondStore.util.RedisConstants.CACHE_SHOP_KEY;

/**
 * 商品(Commodity)表服务实现类
 *
 * @author bwmgd
 * @since 2021-04-25 09:50:11
 */
@Slf4j
@Service("commodityService")
public class CommodityServiceImpl extends ServiceImpl<CommodityDao, Commodity> implements CommodityService {

    @Override
    public List<Commodity> getCommoditiesByShopId(Serializable shopId, Serializable status) {
        Map<SFunction<Commodity, ?>, Serializable> map = new HashMap<>();
        map.put(Commodity::getShopId, shopId);
        map.put(Commodity::getStatus, status);
        return list(new LambdaQueryWrapper<Commodity>().allEq(map));
    }

    @Override
    public List<Commodity> getCommoditiesByKindId(Serializable kindId) {
        Map<SFunction<Commodity, ?>, Serializable> map = new HashMap<>();
        map.put(Commodity::getKindId, kindId);
        map.put(Commodity::getStatus, 1);
        return list(new LambdaQueryWrapper<Commodity>().allEq(map));
    }

//    @Autowired(required=true)


    @Resource
    private StringRedisTemplate stringRedisTemplate;

    protected static final Logger logger = LoggerFactory.getLogger(CommodityServiceImpl.class);

    @Override
    public R queryById(Serializable id) {

        String key = CACHE_SHOP_KEY + id;

        String commodityJson =  stringRedisTemplate.opsForValue().get("cache:commodity:"+id);
        if (StringUtils.isNotBlank(commodityJson)){
            logger.info("Redis hit! => query by Redis(ServiceImpl)");
            Commodity commodity = JSONUtil.toBean(commodityJson, Commodity.class);
            return R.ok(commodity);
        }

        Commodity commodity = getById(id);
        logger.info("Redis miss! => query by Mysql(ServiceImpl)");

        if (commodity == null){
            return R.ok("");
        }

        logger.info("Write to redis(ServiceImpl)");
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(commodity));

        return R.ok(commodity);
    }


}